//
//  MessageViewController.swift
//  WeiBoOfSwift
//
//  Created by aven wu on 20/04/2018.
//  Copyright © 2018 GML. All rights reserved.
//

import UIKit


class MessageViewController: VistorBaseViewController {

    let messageCellID = "messageCellID"
    var dataList:[UserOfMessage]?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setUpNav()
        view.addSubview(tabView)

        // 如果没有登录  设置未登录界面
        if userlogin == false {
            vistorView?.setUpVisitorInfo(isHome: false, imageName: "visitordiscover_image_message", message: "登陆查看最新私信")
        }else{
            UserOfMessage.loadMessageList(finished: { list in
                self.dataList = list!
                self.tabView.reloadData()
            })
            
        }
    }

    private func setUpNav(){
        navigationItem.leftBarButtonItem  = UIBarButtonItem.creatCustomBarButtonItem(imageName: "", highlightedName: "",title:"发现群", target: self, action: #selector(leftBarButtonItemClick))
        
        navigationItem.rightBarButtonItem  = UIBarButtonItem.creatCustomBarButtonItem(imageName: "", highlightedName: "",title:"联系人", target: self, action: #selector(rightBarButtonItemClick))
       
    }
   
    @objc private func leftBarButtonItemClick(){
        
    }
    @objc private func rightBarButtonItemClick(){
      
        
    }
    
    private lazy var tabView:UITableView = {
        let tab = UITableView(frame:CGRect(x:self.view.frame.origin.x, y: self.view.frame.origin.y+SAFE_SCREEN_TOP, width: self.view.bounds.width, height: self.view.bounds.height-SAFE_SCREEN_BOTTOM-SAFE_SCREEN_TOP-44), style: UITableViewStyle.plain)
        tab.separatorStyle = UITableViewCellSeparatorStyle.none
//        tab.register(UITableViewCell.self, forCellReuseIdentifier: messageCellID)
        tab.register(UINib.init(nibName: "MessageHomeCell", bundle: nil), forCellReuseIdentifier: messageCellID)
        if #available(iOS 11.0, *){
            tab.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentBehavior.never
        }
        tab.delegate = self
        tab.dataSource = self
        return tab
    }()

}

//MARK: UITableViewDelegate
extension MessageViewController:UITableViewDelegate,UITableViewDataSource{
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        return dataList?.count ?? 0
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: messageCellID, for: indexPath) as! MessageHomeCell
        cell.model = dataList![indexPath.row]
        cell.selectionStyle = UITableViewCellSelectionStyle.none

        return cell
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        
        return 60.0
    }
}


